from django.contrib.auth.models import User
from jwt import DecodeError
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_jwt.authentication import jwt_decode_handler
from journal.serializers import *
from utils.response import *
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from accounts.models import Info
from journal.models import *


class FractionAPI(APIView):
    Id = openapi.Parameter('id', required=True, in_=openapi.IN_QUERY, description='id',
                           type=openapi.TYPE_STRING)

    @swagger_auto_schema(
        manual_parameters=[Id],
        responses={
            '200': openapi.Response('', FractionSer)
        },
        security=[],
        operation_id='获取学生操勤分信息',
        operation_description='返回学生操勤分信息',
    )
    def get(self, request):
        if request.method == 'GET':
            data = request.GET.get('id')
            try:
                student = Info.objects.get(id=data, station__id=1)
            except Info.DoesNotExist:
                nothing['error'] = '没有找到该学生的信息'
                return Response(nothing)
            else:
                serializer = FractionSer(student)
                response['data'] = serializer.data
                return Response(response)

    Id = openapi.Parameter('id', required=True, in_=openapi.IN_QUERY, description='id',
                           type=openapi.TYPE_STRING)
    Action = openapi.Parameter('action', required=True, in_=openapi.IN_QUERY, description='action',
                               type=openapi.TYPE_STRING)
    Log = openapi.Parameter('log', required=True, in_=openapi.IN_QUERY, description='log',
                            type=openapi.TYPE_STRING)

    @swagger_auto_schema(
        manual_parameters=[Id, Action, Log],
        responses={
            '200': openapi.Response('', FractionSerializers)
        },
        security=[],
        operation_id='更改学生操勤分',
        operation_description='更改学生操勤分信息',
    )
    def post(self, request):
        if request.method == 'POST':
            data = request.POST.get('id')
            action = request.POST.get('action')
            log = request.POST.get('log')
            if log == '':
                nothing['msg'] = '更改分数失败'
                nothing['error'] = '更改分数原因不能为空'
                return Response(nothing)
            else:
                if action == '':
                    nothing['msg'] = '更新分数失败'
                    nothing['error'] = '分数不能为空'
                    return Response(nothing)
                else:
                    student = Info.objects.get(id=data)
                    student.credit += float(action)
                    student.save()
                    fractions = Fraction.objects.create(fraction_id=data, action=action, log=log)
                    serializer = FractionSerializers(fractions)
                    response['msg'] = '更新分数成功'
                    response['data'] = serializer.data
                    return Response(response)


